﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NetworkManagement;

namespace LorraineSec.MessageLib.Messages
{
    public sealed class KeyResponseMessage : Message
    {
        #region Format Constants
        /*
         * Format:
         *          1               x
         * | PublicKey Length | PublicKey |
         */

        private const int PLD_KEYLEN_LENGTH = 1;

        private const int PLD_KEYLEN_OFFSET = 0;
        private const int PLD_KEY_OFFSET = PLD_KEYLEN_OFFSET + PLD_KEYLEN_LENGTH;

        private const int PLD_KNOWN_LENGTH = PLD_KEYLEN_LENGTH;
        #endregion

        public string keyString;

        public KeyResponseMessage()
        {
            init();
        }

        public KeyResponseMessage(byte[] bytes)
            : base(bytes)
        {
            init();
            decode();
        }

        private void init()
        {
            MessageID = (ushort)LorraineSecMessageFactory.MessageID.KEY_RESPONSE;
        }

        public override void encode()
        {
            payload = new byte[PLD_KNOWN_LENGTH + keyString.Length];
            Bytes.setValue(payload, PLD_KEYLEN_OFFSET, (byte)keyString.Length);
            Bytes.setValue(payload, PLD_KEY_OFFSET, keyString);
            base.encode();
        }

        public override void decode()
        {
            base.decode();
            keyString = Bytes.getString(payload, PLD_KEY_OFFSET, Bytes.getByte(payload, PLD_KEYLEN_OFFSET));
        }

    }
}
